const wxapi = require("../../../utils/wxapi");
import { API_SNZO_FILE_URL, API_SNZO_URL } from "../../../utils/wxapi";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    worksItem: null,
    worksTypeOfData: null,
    snzoImgUrl: API_SNZO_FILE_URL
  },

  /**
   * 菜单切换
   * @param {*} e 
   */
  tabChange(e) {
    const tabIndex = e.currentTarget.dataset.tabIndex;
    this.setData({
      tabIndex
    })
  },

  /**
   * 上架艺术品
   * @param {*} e 
   */
  submitWorks(e) {
    const value = e.detail.value;
    if (!value.worksPrice) {
      wx.showModal({
        title: '提示信息',
        content: '请输入交易价格',
        showCancel: false
      })
      return false;
    }
    if (this.data.worksItem.tabStnumWorksMsg.worksType == 24 && value.worksPrice <= 10) {
      wx.showModal({
        title: '提示信息',
        content: '摄影艺术品价格不能低于10元',
        showCancel: false
      })
      return false;
    }
    const bodyParams = {
      id: this.data.worksItem.tabStnumWorksMsg.id,
      worksPrice: value.worksPrice
    }
    const headers = {
      Authorization: app.globalData.token
    }
    wxapi.transactionWorks(bodyParams, headers).then(res => {
      if (res.status === 200) {
        wx.showToast({
          title: '提交成功',
          success: () => {
            wx.switchTab({
              url: '/pages/center/center',
            })
          }
        })
      } else if (res.status === 0) {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/login',
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: '提交失败',
          icon:'error'
        })
      }
    })
  },

  /**
   * 取消艺术品交易
   */
  cancelSubmit() {
    wx.switchTab({
      url: '/pages/center/center',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel();
    if (eventChannel && Object.keys(eventChannel).length > 0) {
      eventChannel.on('worksMsg', (worksMsg) => {
        if (worksMsg) {
          this.setData({
            worksItem: worksMsg.worksItem,
            worksTypeOfData: worksMsg.worksTypeOfData
          });
        } else {
          wx.switchTab({
            url: '/pages/center/center',
          })
        }
      })
    } else {
      wx.switchTab({
        url: '/pages/center/center',
      })
      return;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})